package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.*;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProcessSevenToEightBoatsService {

    @Autowired
    private RaceInfoService raceInfoService;
    @Autowired
    private RaceProcessCommonService raceProcessCommonService;

    public List<RoundScoreVO> getAdvancingToSemis(List<RoundScoreVO> p1List, List<RoundScoreVO> p2List, Long raceId) {
        List<RoundScoreVO> list1 = raceProcessCommonService.sortedEntries(p1List);
        List<RoundScoreVO> list2 = raceProcessCommonService.sortedEntries(p2List);
        List<RoundScoreVO> advancingToSemis = new ArrayList<>();

        int min = Math.min(list1.size(), list2.size());
        for (int i = 1; i < min; i++) {
            if (i % 2 == 0) {
                advancingToSemis.add(list2.get(i));
                advancingToSemis.add(list1.get(i));
            } else {
                advancingToSemis.add(list1.get(i));
                advancingToSemis.add(list2.get(i));
            }
        }
        for (int i = min; i < list1.size(); i++) {
            advancingToSemis.add(list1.get(i));
        }
        for (int i = min; i < list2.size(); i++) {
            advancingToSemis.add(list2.get(i));
        }
        return advancingToSemis;
    }

    public List<RoundScoreVO> getAdvancingToFinals(List<RoundScoreVO> p1List, List<RoundScoreVO> p2List, Long raceId) {
        if (p2List != null) {
            // 获取预赛直接晋级决赛的队伍
            // 获取复赛名单时已排过序，此时无序排序
            List<RoundScoreVO> advancingToFinals = new ArrayList<>();
            advancingToFinals.add(p1List.get(0));
            advancingToFinals.add(p2List.get(0));
            return advancingToFinals;
        }
        // 获取复赛晋级决赛的队伍
        return raceProcessCommonService.sortedEntries(p1List);
    }

    public List<RoundScoreVO> getRank(List<RoundScoreVO> f1List, List<RoundScoreVO> f2List, List<RoundScoreVO> sList, RaceVO raceVO) {
        List<RoundScoreVO> list = raceProcessCommonService.sortedEntries(f1List);
        List<RoundScoreVO> list1 = raceProcessCommonService.sortedEntries(f2List);
        List<RoundScoreVO> list2 = raceProcessCommonService.sortedEntries(sList);

        if (f2List.isEmpty()) {
            list.add(list2.get(list2.size() - 1));
        } else {
            list.addAll(list1);
        }
        saveFinalResult(list, raceVO);
        int i = 0;
        for (RoundScoreVO roundScoreVO : list) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return list;
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
